package com.example.ra.controller;

import com.example.ra.domain.dto.ExportDTO;
import com.example.ra.domain.dto.MarketingDTO;
import com.example.ra.domain.vo.ExportVO;
import com.example.ra.domain.vo.MarketingRecordVO;
import com.example.ra.service.CustRecordsService;
import com.example.ra.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 记录管理接口
 * @Author: whopxx
 * @CreateTime: 2025-03-05
 */
@RestController
@RequestMapping("/record")
@Tag(name = "营销记录管理接口", description = "管理营销记录")
@Slf4j
public class RecordManagementController {
    @Autowired
    private CustRecordsService custRecordsService;

    @Autowired
    private FileUploadService fileUploadService;
    /**
     * 查询某用户id30天内的营销记录
     */
    @PostMapping("/get30")
    @Operation(summary = "查询某用户id30天内的营销记录")
    @ApiResponse(responseCode = "200", description = "查询某用户id30天内的营销记录成功")
    public MarketingRecordVO get30(@ParameterObject @RequestBody MarketingDTO mtd) {
        log.debug("查询某用户{}30天内的营销记录", mtd.getCustId());
        String custId = mtd.getCustId();
        if (custId == null || custId.isEmpty()){
            throw new RuntimeException("custId不能为空");
        }
        return custRecordsService.get30(custId);
    }


    @Autowired
    private RedissonClient redisson;
    /**
     * 客户营销
     */
    @PostMapping("/marketing")
    @Operation(summary = "客户营销", description = "向指定id的客户进行营销")
    @ApiResponse(responseCode = "200", description = "客户营销计划发送成功")
    public boolean marketing(@ParameterObject @RequestBody MarketingDTO mtd) {
        log.debug("向客户{}进行营销", mtd.getCustId());

        RBloomFilter<String> bloomFilter = redisson.getBloomFilter("cust_marketing");
        if (!bloomFilter.contains(mtd.getCustId())) {
            throw new RuntimeException("非法用户营销");
        }
        if (mtd.getCustId() == null || mtd.getCustId().isEmpty()){
            throw new RuntimeException("custId不能为空");
        }
        if (mtd.getSendAccount() == null || mtd.getSendAccount().isEmpty()){
            throw new RuntimeException("sendAccount不能为空");
        }
        custRecordsService.insertCustRecord(mtd);
        return true;
    }

    /**
     * 目标客户导出接口
     */
    @PostMapping("/export")
    @Operation(summary = "目标客户导出接口", description = "按条件导出目标客户信息")
    @ApiResponse(responseCode = "200", description = "目标客户导出接口成功")
    public ExportVO export(@ParameterObject @RequestBody ExportDTO exportDTO, HttpServletResponse response){
        Double time = fileUploadService.build(exportDTO,response);
        return new ExportVO("/record/download/custInfo.zip", time);
    }

    /**
     * 下载目标客户信息
     */
    @GetMapping("/download/custInfo.zip")
    @Operation(summary = "下载目标客户信息", description = "下载目标客户信息")
    @ApiResponse(responseCode = "200", description = "下载目标客户信息成功")
    public void download(HttpServletResponse response){
        fileUploadService.export(response);
    }

}
